package model;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import base.Contacts;

public final class BluetoothContacts implements Contacts {

	private static final String RECORD_STORE_NAME = "MsgContactsRS";
	public static final String NAME_ATTR = "name=";
	public static final String BT_ATTR = "bt=";
	public static final String IMG_ATTR = "img=";

	private RecordStore store;
	
	public BluetoothContacts(MsgContact own) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException {
		store = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
		byte[] bytes = own.toString().getBytes();
		if (store.getNumRecords() > 0) {
			own.id = 1;
			String record = new String(store.getRecord(own.id));
			own.name = getAttribute(record, NAME_ATTR);
			own.image = Integer.parseInt(getAttribute(record, IMG_ATTR));
		} else {
			own.id = store.addRecord(bytes, 0, bytes.length);
		}
		store.closeRecordStore();
	}

	
	public MsgContact[] getAllContacts() {
		int id, size = 0;
		String record;
		MsgContact[] elements;
		try {
			
			store = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
			RecordEnumeration enumeration = store.enumerateRecords(null, null, false);
			size = enumeration.numRecords();
			if (size == 1) {
				store.closeRecordStore();
				return null;
			}
			elements = new MsgContact[size-1];
			size = 0;
			while (enumeration.hasNextElement()) {
				id = enumeration.nextRecordId();
				record = new String(store.getRecord(id));
				elements[size++] = new MsgContact(getAttribute(record, NAME_ATTR), null, -1);
				if ((size + 1) > elements.length)
					break;
			}
			store.closeRecordStore();
			return elements;
		} catch (RecordStoreNotOpenException e) {
		} catch (InvalidRecordIDException e) {
		} catch (RecordStoreException e) {
		}
		return null;
	}

	public boolean getContact(MsgContact contact) {
		try {
			store = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
			String record = new String(store.getRecord(contact.id));
			store.closeRecordStore();
			contact.name = getAttribute(record, NAME_ATTR);
			contact.url = getAttribute(record, BT_ATTR);
			contact.image = Integer.parseInt(getAttribute(record, IMG_ATTR));
			return true;
		} catch (RecordStoreNotOpenException e) {
		} catch (InvalidRecordIDException e) {
		} catch (RecordStoreException e) {
		}
		return false;
	}

	public boolean addContact(MsgContact contact) {
		StringBuffer record = new StringBuffer(256);
		try {
			if (contact.id != -1) return false;
			record.append(NAME_ATTR).append(contact.name).append('_');
			record.append(BT_ATTR).append(contact.url).append('_');
			record.append(IMG_ATTR).append(contact.image).append('_');
			store = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
			contact.id = store.addRecord(record.toString().getBytes(), 0, record.length());
			store.closeRecordStore();
			return true;
		} catch (RecordStoreNotOpenException e) {
		} catch (RecordStoreFullException e) {
		} catch (RecordStoreException e) {
		}
		return false;
	}

	public boolean removeContact(MsgContact contact) {
		try {
			if (contact.id > 0) return false;
			store = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
			store.deleteRecord(contact.id);
			store.closeRecordStore();
			return true;
		} catch (RecordStoreNotOpenException e) {
		} catch (RecordStoreFullException e) {
		} catch (RecordStoreException e) {
		}
		return false;
	}
	
	public boolean setContact(MsgContact contact) {
		StringBuffer record = new StringBuffer(256);
		byte[] bytes;
		try {
			if (contact.id < 0) return false;
			record.append(NAME_ATTR).append(contact.name).append('_');
			record.append(BT_ATTR).append(contact.url).append('_');
			record.append(IMG_ATTR).append(contact.image).append('_');
			store = RecordStore.openRecordStore(RECORD_STORE_NAME, false);
			bytes = record.toString().getBytes();
			store.setRecord(contact.id, bytes, 0, bytes.length);
			return true;
		} catch (RecordStoreFullException e) {
		} catch (RecordStoreNotFoundException e) {
		} catch (RecordStoreException e) {
		}
		return false;
	}
	
	public static String getAttribute(String record, String attr) {
		int begin = record.indexOf(attr) + attr.length();
		int end = record.indexOf('_', begin);
		return record.substring(begin, end);
	}
	
}
